// Please see the documentation in index.html

unit Unit1;

interface

{$IFNDEF SAX_WIDESTRINGS}
This demo requires that you activate SAX_WIDESTRINGS within the Project | Options
Conditional Defines section (after you specify the flag, build all)
{$ENDIF}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ImgList, SAXComps, SAX, BSAX, SAXDE, SAXXMLPartner,
  SAXAElfred2, SAXMS, SAXXDOM, SAXKW;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    ComboBox1: TComboBox;
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    Label3: TLabel;
    ImageList1: TImageList;
    SAXErrorHandler1: TSAXErrorHandler;
    SAXContentHandler1: TSAXContentHandler;
    OpenDialog1: TOpenDialog;
    ListBox1: TListBox;
    SAXXMLPartner1: TSAXXMLPartner;
    SAXDestructor1: TSAXDestructor;
    SAXDelphi1: TSAXDelphi;
    SAXOpenXML1: TSAXOpenXML;
    SAXMSXML1: TSAXMSXML;
    SAXAElfred21: TSAXAElfred2;
    SAXAElfred2XMLReader1: TSAXAElfred2XMLReader;
    procedure FormCreate(Sender: TObject);
    procedure SAXErrorHandler1Error(Sender: TObject;
      const Error: ISAXParseError);
    procedure SAXErrorHandler1FatalError(Sender: TObject;
      const Error: ISAXParseError);
    procedure SAXErrorHandler1Warning(Sender: TObject;
      const Error: ISAXParseError);
    procedure Button1Click(Sender: TObject);
    procedure SAXContentHandler1StartElement(Sender: TObject;
      const NamespaceURI, LocalName, QName: WideString;
      const Atts: IAttributes);
    procedure SAXContentHandler1EndElement(Sender: TObject;
      const NamespaceURI, LocalName, QName: WideString);
  private
    CurrNode : TTreeNode;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses SAXAdapters;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Fill the list
  ListSAXVendors(ComboBox1.Items);
  // Set the item index
  ComboBox1.ItemIndex:= ComboBox1.Items.IndexOf(DefaultSAXVendor);
end;

procedure TForm1.SAXErrorHandler1Error(Sender: TObject;
  const Error: ISAXParseError);
begin
  ListBox1.Items.AddObject('[Error] ' + Error.getMessage + ' Line ' +
    IntToStr(Error.getLineNumber) + ' Column ' + IntToStr(Error.getColumnNumber),
    Pointer(Error.getLineNumber));
end;

procedure TForm1.SAXErrorHandler1FatalError(Sender: TObject;
  const Error: ISAXParseError);
begin
  ListBox1.Items.AddObject('[Fatal Error] ' + Error.getMessage + ' Line ' +
    IntToStr(Error.getLineNumber) + ' Column ' + IntToStr(Error.getColumnNumber),
    Pointer(Error.getLineNumber));
end;

procedure TForm1.SAXErrorHandler1Warning(Sender: TObject;
  const Error: ISAXParseError);
begin
  ListBox1.Items.AddObject('[Warning] ' + Error.getMessage + ' Line ' +
    IntToStr(Error.getLineNumber) + ' Column ' + IntToStr(Error.getColumnNumber),
    Pointer(Error.getLineNumber));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  XMLReader : IXMLReader;
  XMLBufReader: IBufferedXMLReader;
  Vendor: TSAXVendor;
  ContentHandler: IContentHandler;
  Ticks : Integer;
begin
  TreeView1.Items.Clear;
  Listbox1.Items.Clear;
  CurrNode:= nil;
  ContentHandler:= SAXContentHandler1;
  if (OpenDialog1.Execute) then
  begin
    Ticks:= GetTickCount;
    Vendor:= GetSAXVendor(ComboBox1.Items[ComboBox1.ItemIndex]);
    if Vendor is TBufferedSAXVendor then
    begin
      XMLBufReader:= TBufferedSAXVendor(Vendor).BufferedXMLReader;
      XMLBufReader.setContentHandler(Adapt(ContentHandler, XMLBufReader));
      XMLBufReader.setErrorHandler(SAXErrorHandler1);
      XMLBufReader.parse(OpenDialog1.FileName);
      XMLBufReader:= nil;
    end else
    begin
      XMLReader:= Vendor.XMLReader;
      XMLReader.setContentHandler(ContentHandler);
      XMLReader.setErrorHandler(SAXErrorHandler1);
      XMLReader.parse(OpenDialog1.FileName);
      XMLReader:= nil;
    end;
    Caption:= 'Filling a TreeView - ' + IntToStr(GetTickCount-Ticks) + 'ms';
  end;
end;

procedure TForm1.SAXContentHandler1StartElement(Sender: TObject;
  const NamespaceURI, LocalName, QName: WideString;
  const Atts: IAttributes);
var I : Integer;
    ANode : TTreeNode;
begin
  // Set the node
  if (CurrNode = nil) then
    CurrNode:= TreeView1.Items.Add(nil, qName)
  else
    CurrNode:= TreeView1.Items.AddChild(CurrNode, qName);
  // Set the image
  CurrNode.ImageIndex:= 0;
  // Add the attributes
  if (Atts <> nil) then
  begin
    for I:= 0 to Atts.getLength - 1 do
    begin
      ANode:= TreeView1.Items.AddChild(CurrNode, Atts.getQName(I));
      ANode.ImageIndex:= 1;
      ANode.SelectedIndex:= 1;
    end;
  end;
end;

procedure TForm1.SAXContentHandler1EndElement(Sender: TObject;
  const NamespaceURI, LocalName, QName: WideString);
begin
  // Expand the item
  if (CurrNode <> nil) then
    CurrNode.Expand(false);
  // Set the node
  CurrNode:= CurrNode.Parent;
end;

end.
